package com.pic.mgt.monitor.parser;

import org.apache.commons.lang.StringUtils;

import com.pic.mgt.monitor.WrongUnitFormatException;

public final class businessSerialNumberParser extends	FormatParser {
	private int businessSNStartFromChar;
	private int businessSNEndToChar;	
	
	public businessSerialNumberParser() {
		
	}

	@Override
	public String parse(Object _confirmationFilename)
							throws WrongUnitFormatException {
		// 確認是否為 String 型態
		// throws WrongUnitFormatException
		super.checkObjectType(_confirmationFilename, String.class);
		
		String confirmationFilename = (String)_confirmationFilename;
		
		String businessSN = null;
		// 取得 Serial Number
		try {
			//throws StringIndexOutOfBoundsException
			businessSN = confirmationFilename.
							substring(this.businessSNStartFromChar - 1,
									  this.businessSNEndToChar);
		} catch (StringIndexOutOfBoundsException e) {			
			throw new WrongUnitFormatException(
					"Confirmation filename format is invalid.", e);
		}
		// 驗證是否為數字
		boolean isDigital = StringUtils.isNumeric(businessSN);
		if (!isDigital) {
			throw new WrongUnitFormatException(
					"The business serial number '" + businessSN +
					"' must be digital.");
		}
		
		return businessSN;
	}

	public void setBusinessSNStartFromChar(int businessSNStartFromChar) {
		this.businessSNStartFromChar = businessSNStartFromChar;
	}

	public void setBusinessSNEndToChar(int businessSNEndToChar) {
		this.businessSNEndToChar = businessSNEndToChar;
	}

}
